'use strict';

const Service = require('egg').Service;
const { stateKeys } = require('../core/base_mongo_model');

const {
  STUDY_DETAIL_CATCE_KEY,
  STUDY_LIST_CATCE_KEY,
  STUDY_LIST_DUSTBIN_CATCE_KEY,
  STUDY_ME_LIST_CATCE_KEY,
  STUDY_TYPES_CATCE_KEY
} = require('../../config/redisKey');

class StudyService extends Service {

  // 详情缓存的KEY
  deatilCatchKey(id) {
    return `${STUDY_DETAIL_CATCE_KEY}_${id}`
  }

  /**
   * 获取我的列表里面的数据
   */
  async me() {
    return await this.ctx.getCache(STUDY_ME_LIST_CATCE_KEY, async () => {
      const field = ['title', 'date', 'type', 'describe', 'pic'];
      const model = this.ctx.model.Study.find({
        stateKey: stateKeys.NORMAL
      }, field).skip(0).limit(6);
      model.sort({ date: -1 });
      return model.exec();
    })
  }
  /**
   * 获取列表数据
   */
  async list() {
    return this.ctx.model.Study.findList(this.ctx.query);
  }

  /**
   * 获取总个数
   */
  async count() {
    return await this.ctx.model.Study.findCount(this.ctx.query);
  }

  /**
   * 获取类型
   */
  async types() {
    return await this.ctx.getListCache(STUDY_TYPES_CATCE_KEY, async () => {
      return await this.ctx.model.Study.findTypes();
    }, true)
  }

  /**
   * 获取详情
   */
  async detail() {
    const { id } = this.ctx.params
    return await this.ctx.getListCache(this.deatilCatchKey(id), async () => {
      return await this.ctx.model.Study.findDetail(id);
    })
  }

  /**
   * 创建
   */
  async create() {
    this.ctx.clearCache(
      STUDY_LIST_CATCE_KEY,
      STUDY_ME_LIST_CATCE_KEY,
      STUDY_TYPES_CATCE_KEY
    )
    const { Study } = this.ctx.model;
    const model = { ...this.ctx.w_body() };
    model.date = new Date();
    const data = await new Study(model).save();
    return data;
  }

  /**
   * 更新数据
   */
  async update() {
    const {id} = this.ctx.params
    this.ctx.clearCache(
      this.deatilCatchKey(id),
      STUDY_LIST_CATCE_KEY,
      STUDY_ME_LIST_CATCE_KEY,
      STUDY_TYPES_CATCE_KEY
    )
    const { Study } = this.ctx.model;
    return await Study.updateModel(id, this.ctx.w_body());
  }

  /**
   * 删除数据
   */
  async destroy() {
    this.ctx.clearCache(
      STUDY_LIST_CATCE_KEY,
      STUDY_LIST_DUSTBIN_CATCE_KEY,
      STUDY_ME_LIST_CATCE_KEY,
      STUDY_TYPES_CATCE_KEY
    )
    const { Study } = this.ctx.model;
    return await Study.removeModel(this.ctx.params.id);
  }

  /**
   * 获取回收站列表
   */
  async dustbinList() {
    return this.ctx.getCache(STUDY_LIST_DUSTBIN_CATCE_KEY, async () => {
      return await this.ctx.model.Study.find({
        $or: [
          { stateKey: stateKeys.DUSTBIN },
        ]
      }).exec();
    })
  }
  /**
   * 从回收站还原
   */
  async dustbinRestore() {
    this.ctx.clearCache(
      STUDY_LIST_CATCE_KEY,
      STUDY_LIST_DUSTBIN_CATCE_KEY,
      STUDY_ME_LIST_CATCE_KEY,
      STUDY_TYPES_CATCE_KEY
    )
    const { Study } = this.ctx.model;
    const { id } = this.ctx.params;
    return await Study.dustbinRestore(id);
  }
  /**
   * 从回收站删除数据
   */
  async dustbinDelete() {
    this.ctx.clearCache(
      STUDY_LIST_DUSTBIN_CATCE_KEY,
    )
    const { Study } = this.ctx.model;
    return await Study.removeModel(this.ctx.params.id, true);
  }
}

module.exports = StudyService;
